#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    :   stock_market.py
@Time    :   2023/03/29 23:04:31
@Author  :   youle
@Email   :   a_fly0505@163.com
@Copyright : 侵权必究
'''

import time
from sqlalchemy import Boolean, Column, Date, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from core.config import Config

Base = declarative_base()
metadata = Base.metadata


class Stock(Base):
    __tablename__ = 'stock'
    __table_args__ = {'schema': 'stock_market'}

    code = Column(String(32), primary_key=True)
    sname = Column(String(128), nullable=False)
    exchange = Column(String(128))
    ah = Column(Boolean)
    entry_time = Column(Date)


class ModelStock():
    def __init__(self, config: Config) -> None:
        url = 'postgres://{}:{}@{}:{}/{}'.format(
            config.user, config.password, config.host, config.port, config.database)
        self.engine = create_engine(url)
        self.dbSession = sessionmaker(bind=self.engine)

    def CreateTable(self):
        # 创建表
        Base.metadata.create_all(self.engine)
        session = self.dbSession()
        session.
 
 
    # def insertData(self):
    #     # 插入操作
    #     # 创建会话
    #     session = self.dbSession()
    #     # 创建新User对象:
    #     local_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    #     new_user = User(name='hsh4', sex='男', nation='汉', birth='19991012', id_address='广州大学', id_number='441242142142',
    #                     create_time=local_time)
    #     # 添加到session:
    #     session.add(new_user)
    #     # 提交即保存到数据库:
    #     session.commit()
    #     # 关闭session:
    #     session.close()

    # def selectData(self):
    #     # 查询操作
    #     # 创建Session
    #     session = DBSession()
    #     # 创建Query查询，filter是where条件，最后调用one()返回唯一行，如果调用all()则返回所有行:
    #     user = session.query(User).filter(User.id == '1' and User.name == 'hsh4').one()
    #     print('name:', user.name)
    #     print('id_address:', user.id_address)
    #     session.close()  # 关闭Session
    
    # def updateData(self):
    #     # 更新操作
    #     session = DBSession()  # 创建会话
    #     users = session.query(User).filter_by(name="hsh4").first()  # 查询条件
    #     users.id_number = "abcd"  # 更新操作
    #     session.add(users)  # 添加到会话
    #     session.commit()  # 提交即保存到数据库
    #     session.close()  # 关闭会话
    
    # def deleteData(self):
    #     # 删除操作
    #     session = DBSession()  # 创建会话
    #     delete_users = session.query(User).filter(User.id == "1").first()
    #     if delete_users:
    #         session.delete(delete_users)
    #         session.commit()
    #     session.close()  # 关闭会话
    
    # def dropTable(self):
    #     # 删除表
    #     session = DBSession()  # 创建会话
    #     session.execute('drop table users')
    #     session.commit()
    #     session.close()
